import {Component, OnInit, Input, Output, EventEmitter, OnChanges, OnDestroy} from '@angular/core';
import {IndexService} from "../../main/services/index.service";
import {TipService} from "../../main/services/tip.service";
import {Subscribable} from "rxjs/Observable";
import {Subscription} from "rxjs";

@Component({
  selector: 'app-standard-dialog',
  templateUrl: './standard-dialog.component.html',
  styleUrls: ['./standard-dialog.component.css']
})
export class StandardDialogComponent implements OnInit, OnChanges, OnDestroy {

  // 控制背景遮罩
  @Input() show: boolean = false;

  // 弹框显示关闭
  @Output() showHideChange = new EventEmitter();

  // 当前操作的菜品
  @Input() foodItem: any;

  // 单选的数据结构
  radioStructure = {sort: 0, price: 0, value: '', count: 0};
  tempRadioStructure = {sort: 0, price: 0, value: '', count: 0};

  // 多选的数据结构
  mutilStructure = {sort: 0, value: '', count: 0};

  // 临时用于多选存储value值
  tempMutilSelect = [];

  // 抽选数据结构
  pumpStructure = {sort: 0, value: '', count: 0};

  // 临时用于抽选存储value值
  tempPumpSelect = [];

  subscribeClearData:Subscription;


  constructor(private  indexServices: IndexService, private tipService: TipService,) {
  }

  ngOnInit() {
  }

  ngOnDestroy() {
  }

  ngOnChanges() {

    // 清除临时多选项value
    this.tempMutilSelect = [];

    // 接收属性值发生变化，解析choose_json(规格类型)
    let transformJson = JSON.parse(this.foodItem['choose_json']);

    transformJson.list.forEach((v, k) => {

      // 当前菜品的类型是单选，把当前第一个的值取出来，第一个处于选中状态（必须选一个）
      if (this.foodItem.type == '1') {
        if (k === 0) {
          this.radioStructure.price = v.price;
          this.tempRadioStructure.price = v.price;
          this.radioStructure.value = v.value;
          this.tempRadioStructure.value = v.value;
        }
        v['sort'] = k;
      }
    });
    this.foodItem['transformJson'] = transformJson;
  }

  // 关闭窗口
  close() {
    this.showHideChange.emit(false);
  }


  /* ---------------------------------------------------------- 单选相关的操作开始 ---------------------------------------------------------------------- */

  // 单选操作
  changeRadioItem(item) {
    this.radioStructure.sort = item.sort;
    this.radioStructure.value = item.value;
    this.radioStructure.price = item.price;
  }

  // 单选加入购物车
  radioAddCart() {

    if (this.radioStructure.value) {
      if (this.foodItem.typeList.radioType.length) {
        let tempArray = this.foodItem.typeList.radioType;
        let flag = true;
        tempArray.forEach(v => {

          // 有数据并且找到本条数据
          if (v.value == this.radioStructure.value) {
            // console.log('有数据并且找到本条数据');
            v.count++;
            flag = false;
            return;
          }
        });

        // 有数据没有本条数据推进去
        if (flag) {
          // console.log('有数据没有本条数据推进去')
          this.radioStructure.count = 1;
          this.foodItem.typeList.radioType.push(Object.assign({}, this.radioStructure));
        }
      } else { // 没有数据推进去
        // console.log('没有数据推进去');
        this.radioStructure.count = 1;
        this.foodItem.typeList.radioType.push(Object.assign({}, this.radioStructure));
      }

      // 添加完成之后修改菜品的数量
      let tempCount = 0;
      this.foodItem.typeList.radioType.forEach(v3 => {
        tempCount += v3.count;
      });
      if (tempCount) {
        this.foodItem.count = tempCount;
      }

      // 操作完成之后更新菜品
      this.indexServices.updateItem(this.foodItem);

      this.radioStructure = Object.assign({}, this.tempRadioStructure);
    }

    this.showHideChange.emit(false);
  }

  /* ---------------------------------------------------------- 单选相关的操作结束 ---------------------------------------------------------------------- */


  /* ---------------------------------------------------------- 多选相关的操作开始 ---------------------------------------------------------------------- */

  // 多选操作
  changeMutilItem(item) {

    if (this.tempMutilSelect.length) {

      // 存在
      if (this.tempMutilSelect.indexOf(item.value) != -1) {
        let index = this.tempMutilSelect.indexOf(item.value);
        this.tempMutilSelect.splice(index, 1);
      }
      // 不存在
      else {
        this.tempMutilSelect.push(item.value);
      }

    } else {
      this.tempMutilSelect.push(item.value);
    }

  }

  // 多选加入购物车
  mutilAddCart() {

    this.mutilStructure.value = this.tempMutilSelect.join(',');

    if (this.foodItem.typeList.multiType.length) {
      let tempArray = this.foodItem.typeList.multiType;
      let flag = true;
      tempArray.forEach(v => {

        // 有数据并且找到本条数据
        if (v.value == this.mutilStructure.value) {
          // console.log('有数据并且找到本条数据');
          v.count++;
          flag = false;
          return;
        }
      });

      // 有数据没有本条数据推进去
      if (flag) {
        // console.log('有数据没有本条数据推进去')
        this.mutilStructure.count = 1;
        this.foodItem.typeList.multiType.push(Object.assign({}, this.mutilStructure));
      }
    } else { // 没有数据推进去
      // console.log('没有数据推进去');
      this.mutilStructure.count = 1;
      this.foodItem.typeList.multiType.push(Object.assign({}, this.mutilStructure));
    }

    // 添加完成之后修改菜品的数量
    let tempCount = 0;
    this.foodItem.typeList.multiType.forEach(v3 => {
      tempCount += v3.count;
    });
    if (tempCount) {
      this.foodItem.count = tempCount;
    }

    // 操作完成之后更新菜品
    this.indexServices.updateItem(this.foodItem);

    this.tempMutilSelect = [];

    this.showHideChange.emit(false);
  }

  /* ---------------------------------------------------------- 多选相关的操作结束 ---------------------------------------------------------------------- */


  /* ---------------------------------------------------------- 抽选相关的操作开始 ---------------------------------------------------------------------- */

  // 抽选操作
  changePumpItem(item) {

    let first = this.foodItem.transformJson.first, last = this.foodItem.transformJson.last;

    if (this.tempPumpSelect.length) {

      // 存在
      if (this.tempPumpSelect.indexOf(item.value) != -1) {
        let index = this.tempPumpSelect.indexOf(item.value);
        this.tempPumpSelect.splice(index, 1);
      }
      // 不存在
      else {
        if (this.tempPumpSelect.length == first) {
          this.tipService.change(true, `${last}项中最多选${first}项`);
          return;
        }
        this.tempPumpSelect.push(item.value);
      }

    } else {
      if (this.tempPumpSelect.length == first) {
        this.tipService.change(true, `${last}项中最多选${first}项`);
        return;
      }
      this.tempPumpSelect.push(item.value);
    }



  }

  // 抽选加入购物车
  pumpAddCart() {

    let first = this.foodItem.transformJson.first, last = this.foodItem.transformJson.last;

    if (first != this.tempPumpSelect.length) {
      this.tipService.change(true, `${last}项中必须选${first}项`);
      return;
    }

    this.pumpStructure.value = this.tempPumpSelect.join(',');

    if (this.foodItem.typeList.pumpType.length) {
      let tempArray = this.foodItem.typeList.pumpType;
      let flag = true;
      tempArray.forEach(v => {

        // 有数据并且找到本条数据
        if (v.value == this.pumpStructure.value) {
          // console.log('有数据并且找到本条数据');
          v.count++;
          flag = false;
          return;
        }
      });

      // 有数据没有本条数据推进去
      if (flag) {
        // console.log('有数据没有本条数据推进去')
        this.pumpStructure.count = 1;
        this.foodItem.typeList.pumpType.push(Object.assign({}, this.pumpStructure));
      }
    } else { // 没有数据推进去
      // console.log('没有数据推进去');
      this.pumpStructure.count = 1;
      this.foodItem.typeList.pumpType.push(Object.assign({}, this.pumpStructure));
    }

    // 添加完成之后修改菜品的数量
    let tempCount = 0;
    this.foodItem.typeList.pumpType.forEach(v3 => {
      tempCount += v3.count;
    });
    if (tempCount) {
      this.foodItem.count = tempCount;
    }

    // console.log(this.foodItem)
    // 操作完成之后更新菜品
    this.indexServices.updateItem(this.foodItem);

    this.tempPumpSelect = [];
    this.showHideChange.emit(false);
  }

  /* ---------------------------------------------------------- 抽选相关的操作结束 ---------------------------------------------------------------------- */

}
